<?php
class PackageFacilityController extends CBaseController
{		
	
	public function __construct($config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->reqLogin();
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
		$this->templator->assign('menu','facility');
	}
	
	
	public function assign()
	{
		$id = $_POST['id'];
		$facility_ids = $_POST['facility_ids'];
		$package = R::load('travelpackage',$id);		
		foreach($facility_ids as $facility_id)
		{
			$facility = R::load('facility',$facility_id);
			R::associate( $package, $facility );
		}
		$this->facilitys($id);
	}
	
	public function unassign()
	{
		$id = $_GET['id'];
		$package = R::load('travelpackage',$id);
		$facility_id = $_GET['facility_id'];
		$facility = R::load('facility',$facility_id);
		R::unassociate( $package, $facility );
		$this->facilitys();
	}
	
	public function facilitys($id=null)
	{
		if(!isset($id))
		$id = $_GET['id'];
		$package = R::load('travelpackage',$id);
		$assignedFacilitys= R::related( $package, 'facility' );
		$this->templator->assign('package',$package);		
		$this->templator->assign('facilitys',array_merge($assignedFacilitys, array()));
		$this->templator->display('facility/package_facility.html');
	}
	
	public function select()
	{
		$id = $_POST['id'];
		$package = R::load('travelpackage',$id);
		$assignedFacilitys= R::related( $package, 'facility' );		
		$allFacilitys = R::findAll('facility');
		$facilitys =array_diff($allFacilitys, $assignedFacilitys);
		$this->templator->assign('package',$package);
		$this->templator->assign('facilitys',array_merge($facilitys, array()));
		$this->templator->display('facility/facility_list.html');
	}
	
}	